#include "Base.h"
#include "LobbyProtocol.h"

BOOL IsNumber(const char *str)
{
	char *szAll="0123456789";
	int nLen = (int)strlen(str);
	for(int i = 0; i < nLen; i++)
	{
		if(!strchr(szAll, str[i]))
		{
			return FALSE;
		}
	}
	return TRUE;
}

BOOL IsAlphabetOrNumber(const char *str)
{
	char *szAll="_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	int nLen = (int)strlen(str);
	for(int i = 0; i < nLen; i++)
	{
		if(!strchr(szAll, str[i]))
		{
			return FALSE;
		}
	}
	return TRUE;
}

BOOL IsNonQuotation(const char *str)
{
	int nLen = (int)strlen(str);
	for(int i = 0; i < nLen; i++)
	{
		if(str[i] == 39)
		{
			return FALSE;
		}
	} 
	return TRUE;
}

BOOL IsValidUserName(const char *str)
{
	int len = strlen(str);
	if(len < 4 || len > Max_UserName)
		return FALSE;
	return IsNonQuotation(str);
}

BOOL IsValidPassword(const char *str)
{
	int nLen = (int)strlen(str);
	if(nLen < 4)
	{
		return FALSE;
	}
	for(int i = 0; i < nLen; i++)
	{
		if((str[i] < 32) || (str[i] > 126) || (str[i] == 39))
		{
			return FALSE;
		}
	} 
	return TRUE;
}

BOOL IsValidFullName(const char *str)
{
    return IsNonQuotation(str);
}

BOOL IsValidIdentifyCard(const char *str)
{
	if(strlen(str) != 18)
	{
		return FALSE;
	}
	return IsNumber(str);
}

BOOL IsValidBirthday(const char *str)
{
	if(str == NULL || *str == '\0') return 0;
	const char *p = str;
	int i = 0;
	int arrNum[3];
	do 
	{
		arrNum[i++] = atoi(p);
		while(*p >= '0' && *p <= '9')
		{
			p++;
		}

		if(i >= 3)
		{
			if(*p != '\0')
			{
				return FALSE;
			}
			else
			{
				break;
			}
		}

		if(*p == '-')
		{
			p++;
		}
		else
		{
			return FALSE;
		}

	} while (*p != '\0' && i < 3);

	if( i != 3)
	{
		return FALSE;
	}

	if(arrNum[0] < 1900 || arrNum[0] > 2100)
	{
		return FALSE;
	}

	if(arrNum[1] < 1 || arrNum[1] > 12)
	{
		return FALSE;
	}

	if(arrNum[2] < 1 || arrNum[2] > 31)
	{
		return FALSE;
	}

	return TRUE;
}
BOOL IsValidCountry(const char *str)
{
    return IsNonQuotation(str);
}
BOOL IsValidProvince(const char *str)
{
	return IsNonQuotation(str);
}

BOOL IsValidEmail(const char *str)
{
	char *szAll="-_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@.";
	int nLen = (int)strlen(str);
	for(int i = 0; i < nLen; i++)
	{
		if(!strchr(szAll, str[i]))
		{
			return FALSE;
		}
	}
	return TRUE;
}

BOOL LCVerifyCodeMsg::Read( CSocketStream& iStream )
{
    iStream.Read((char*)&m_nLen, sizeof(m_nLen));
	if(m_nLen > 0)
	{
		m_pszImageData = new char[m_nLen];
        iStream.Read((char*)m_pszImageData, m_nLen);
	}
	return TRUE;
}

BOOL LCVerifyCodeMsg::Write( CSocketStream& oStream ) const
{
    oStream.Write((char*)&m_nLen, sizeof(m_nLen));
	if(m_nLen > 0)
	{
        oStream.Write((char*)m_pszImageData, m_nLen);
	}
	return TRUE;
}

BOOL CLRegisterUserMsg::Read( CSocketStream& iStream )
{
    iStream.Read((char*)&m_szUserName, sizeof(m_szUserName));
	iStream.Read((char*)&m_szPwd, sizeof(m_szPwd));
	iStream.Read((char*)&m_nSex, sizeof(m_nSex));
	iStream.Read((char*)&m_szFullName, sizeof(m_szFullName));
	iStream.Read((char*)&m_szIdentityCard, sizeof(m_szIdentityCard));
	iStream.Read((char*)&m_szBirthday, sizeof(m_szBirthday));
	iStream.Read((char*)&m_szCountry, sizeof(m_szCountry));
	iStream.Read((char*)&m_szProvince, sizeof(m_szProvince));
	iStream.Read((char*)&m_szEmail, sizeof(m_szEmail));
	iStream.Read((char*)&m_szVerifyCode, sizeof(m_szVerifyCode));
	iStream.Read((char*)&m_nFigure, sizeof(m_nFigure));
	return TRUE;
}

BOOL CLRegisterUserMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_szUserName, sizeof(m_szUserName));
	oStream.Write((char*)&m_szPwd, sizeof(m_szPwd));
	oStream.Write((char*)&m_nSex, sizeof(m_nSex));
	oStream.Write((char*)&m_szFullName, sizeof(m_szFullName));
	oStream.Write((char*)&m_szIdentityCard, sizeof(m_szIdentityCard));
	oStream.Write((char*)&m_szBirthday, sizeof(m_szBirthday));
	oStream.Write((char*)&m_szCountry, sizeof(m_szCountry));
	oStream.Write((char*)&m_szProvince, sizeof(m_szProvince));
	oStream.Write((char*)&m_szEmail, sizeof(m_szEmail));
	oStream.Write((char*)&m_szVerifyCode, sizeof(m_szVerifyCode));
	oStream.Write((char*)&m_nFigure, sizeof(m_nFigure));
	return TRUE;
}

BOOL LCRegisterUserMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	return TRUE;
}

BOOL LCRegisterUserMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	return TRUE;
}

BOOL CLUpdateUserMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_szUserName, sizeof(m_szUserName));
	iStream.Read((char*)&m_nSex, sizeof(m_nSex));
	iStream.Read((char*)&m_szFullName, sizeof(m_szFullName));
	iStream.Read((char*)&m_szIdentityCard, sizeof(m_szIdentityCard));
	iStream.Read((char*)&m_szBirthday, sizeof(m_szBirthday));
	iStream.Read((char*)&m_szCountry, sizeof(m_szCountry));
	iStream.Read((char*)&m_szProvince, sizeof(m_szProvince));
	iStream.Read((char*)&m_szEmail, sizeof(m_szEmail));
	iStream.Read((char*)&m_nFigure, sizeof(m_nFigure));
	return TRUE;
}

BOOL CLUpdateUserMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_szUserName, sizeof(m_szUserName));
	oStream.Write((char*)&m_nSex, sizeof(m_nSex));
	oStream.Write((char*)&m_szFullName, sizeof(m_szFullName));
	oStream.Write((char*)&m_szIdentityCard, sizeof(m_szIdentityCard));
	oStream.Write((char*)&m_szBirthday, sizeof(m_szBirthday));
	oStream.Write((char*)&m_szCountry, sizeof(m_szCountry));
	oStream.Write((char*)&m_szProvince, sizeof(m_szProvince));
	oStream.Write((char*)&m_szEmail, sizeof(m_szEmail));
	oStream.Write((char*)&m_nFigure, sizeof(m_nFigure));
	return TRUE;
}

BOOL LCUpdateUserMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	return TRUE;
}

BOOL LCUpdateUserMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	return TRUE;
}

BOOL CLUpdatePasswordMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_szUserName, sizeof(m_szUserName));
	iStream.Read((char*)&m_szOldPwd, sizeof(m_szOldPwd));
	iStream.Read((char*)&m_szNewPwd, sizeof(m_szNewPwd));
	return TRUE;
}

BOOL CLUpdatePasswordMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_szUserName, sizeof(m_szUserName));
	oStream.Write((char*)&m_szOldPwd, sizeof(m_szOldPwd));
	oStream.Write((char*)&m_szNewPwd, sizeof(m_szNewPwd));
	return TRUE;
}

BOOL LCUpdatePasswordMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	return TRUE;
}

BOOL LCUpdatePasswordMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	return TRUE;
}

BOOL CLUserLoginMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_stUserLoginParam, sizeof(m_stUserLoginParam));
	iStream.Read((char*)&m_szEncryptInfo, sizeof(m_szEncryptInfo));
	return TRUE;
}

BOOL CLUserLoginMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_stUserLoginParam, sizeof(m_stUserLoginParam));
	oStream.Write((char*)&m_szEncryptInfo, sizeof(m_szEncryptInfo));
	return TRUE;
}

BOOL LCUserLoginMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	if(m_nResult == Load_User_Success)
	{
		iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
		iStream.Read((char*)&m_szUserName, sizeof(m_szUserName));
		iStream.Read((char*)&m_nFigure, sizeof(m_nFigure));
		iStream.Read((char*)&m_nMoney, sizeof(m_nMoney));
		iStream.Read((char*)&m_nLevel, sizeof(m_nLevel));
		iStream.Read((char*)&m_nExp, sizeof(m_nExp));
	}
	return TRUE;
}

BOOL LCUserLoginMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	if(m_nResult == Load_User_Success)
	{
		oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
		oStream.Write((char*)&m_szUserName, sizeof(m_szUserName));
		oStream.Write((char*)&m_nFigure, sizeof(m_nFigure));
		oStream.Write((char*)&m_nMoney, sizeof(m_nMoney));
		oStream.Write((char*)&m_nLevel, sizeof(m_nLevel));
		oStream.Write((char*)&m_nExp, sizeof(m_nExp));
	}
	return TRUE;
}

BOOL CLGetProductListMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	return TRUE;
}

BOOL CLGetProductListMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	return TRUE;
}

BOOL LCProductListMsg::Read( CSocketStream& iStream )
{
	int nCount = 0;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		ProductInfo *pInfo = new ProductInfo;
		pInfo->Read(iStream);
		m_listProductInfo.AddTail(pInfo);
	}
	return TRUE;
}

BOOL LCProductListMsg::Write( CSocketStream& oStream ) const
{
	int nCount = m_listProductInfo.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		ProductInfo *pInfo = m_listProductInfo.GetAt(i);
		pInfo->Write(oStream);
	}
	return TRUE;
}

BOOL CLSignupMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
	iStream.Read((char*)&m_szUserName, sizeof(m_szUserName));
	iStream.Read((char*)&m_dwSignupIP, sizeof(m_dwSignupIP));
	return TRUE;
}

BOOL CLSignupMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	oStream.Write((char*)&m_szUserName, sizeof(m_szUserName));
	oStream.Write((char*)&m_dwSignupIP, sizeof(m_dwSignupIP));
	return TRUE;
}

BOOL LCSignupMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	iStream.Read((char*)&m_szResult, sizeof(m_szResult));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
	iStream.Read((char*)&m_nSignupPlayerCount, sizeof(m_nSignupPlayerCount));
	return TRUE;
}

BOOL LCSignupMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	oStream.Write((char*)&m_szResult, sizeof(m_szResult));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	oStream.Write((char*)&m_nSignupPlayerCount, sizeof(m_nSignupPlayerCount));
	return TRUE;
}

BOOL CLSignoffMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
	return TRUE;
}

BOOL CLSignoffMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	return TRUE;
}

BOOL LCSignoffMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	iStream.Read((char*)&m_nResult, sizeof(m_nResult));
	iStream.Read((char*)&m_szResult, sizeof(m_szResult));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
	iStream.Read((char*)&m_nSignupPlayerCount, sizeof(m_nSignupPlayerCount));
	return TRUE;
}

BOOL LCSignoffMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	oStream.Write((char*)&m_nResult, sizeof(m_nResult));
	oStream.Write((char*)&m_szResult, sizeof(m_szResult));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	oStream.Write((char*)&m_nSignupPlayerCount, sizeof(m_nSignupPlayerCount));
	return TRUE;
}

BOOL LCSignupInfoMsg::Read( CSocketStream& iStream )
{
	int nCount;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		UserSignupInfo info;
		iStream.Read((char*)&info, sizeof(info));	
		m_listSignupInfo.AddTail(info);
	}
	return TRUE;
}

BOOL LCSignupInfoMsg::Write( CSocketStream& oStream ) const
{
	int nCount = m_listSignupInfo.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		UserSignupInfo &info = m_listSignupInfo.GetAt(i);
		oStream.Write((char*)&info, sizeof(info));	
	}
	return TRUE;
}

BOOL LCStartMatchMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_szGsIP, sizeof(m_szGsIP));
	iStream.Read((char*)&m_nPort, sizeof(m_nPort));
	iStream.Read((char*)&m_nEncryptParamLen, sizeof(m_nEncryptParamLen));
	iStream.Read((char*)&m_byEncryptParam, sizeof(m_byEncryptParam));
	
	return TRUE;
}

BOOL LCStartMatchMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_tmMatchStartTime, sizeof(m_tmMatchStartTime));
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_szGsIP, sizeof(m_szGsIP));
	oStream.Write((char*)&m_nPort, sizeof(m_nPort));
	oStream.Write((char*)&m_nEncryptParamLen, sizeof(m_nEncryptParamLen));
	oStream.Write((char*)&m_byEncryptParam, sizeof(m_byEncryptParam));
	return TRUE;
}

BOOL LCSyncUpdateUserMsg::Read( CSocketStream& iStream )
{
	int nCount;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		CUserAttr attr;
		iStream.Read((char*)&attr, sizeof(attr));
		m_listUserAttr.AddTail(attr);
	}

	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		CUserItem item;
		iStream.Read((char*)&item, sizeof(item));
		m_listItem.AddTail(item);
	}
	return TRUE;
}

BOOL LCSyncUpdateUserMsg::Write( CSocketStream& oStream ) const
{
	int nCount = m_listUserAttr.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		CUserAttr &attr = m_listUserAttr.GetAt(i);
		oStream.Write((char*)&attr, sizeof(attr));
	}

	nCount = m_listItem.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		CUserItem &item = m_listItem.GetAt(i);
		oStream.Write((char*)&item, sizeof(item));
	}
	return TRUE;
}